package com.tim.app.ui.activity.setting;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.application.library.util.PackageUtil;
import com.qiyukf.unicorn.api.Unicorn;
import com.tim.app.R;
import com.tim.app.RT;
import com.tim.app.manager.WindowUtils;
import com.tim.app.server.logic.UserManager;
import com.tim.app.ui.activity.BaseActivity;
import com.tim.app.ui.activity.person_center.BlackListActivity;
import com.tim.app.ui.dialog.TipDialog;
import com.tim.app.util.FileSizeUtil;
import com.tim.app.util.MarketUtil;
import com.tim.app.util.ToastUtil;
import com.tim.app.util.ViewGT;

public class SettingActivity extends BaseActivity {

    private static final String TAG = "SettingActivity";

    private ImageButton ibBack;
    private RelativeLayout rlMessage;
    private RelativeLayout rlClearCache;
    private TextView tvCacheNumber;
    private RelativeLayout rlBlackList;
    private RelativeLayout rlScore;
    private RelativeLayout rlLogout;

    MarketUtil marketUtil;

    /**
     * 启动设置界面的统一接口
     *
     * @param context
     */
    public static void start(Context context) {
        Intent intent = new Intent(context, SettingActivity.class);
        context.startActivity(intent);
    }

    private TipDialog tipDialog;//弹框

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    public void initView() {
        ibBack = (ImageButton) findViewById(R.id.ibBack);
        rlMessage = (RelativeLayout) findViewById(R.id.rlMessage);
        rlClearCache = (RelativeLayout) findViewById(R.id.rlClearCache);
        tvCacheNumber = (TextView) findViewById(R.id.tvCacheNumber);
        rlScore = (RelativeLayout) findViewById(R.id.rlScore);
        rlLogout = (RelativeLayout) findViewById(R.id.rlLogout);
        rlBlackList = (RelativeLayout) findViewById(R.id.rlBlackList);
        ibBack.setOnClickListener(this);
        rlMessage.setOnClickListener(this);
        rlClearCache.setOnClickListener(this);
        rlScore.setOnClickListener(this);
        rlLogout.setOnClickListener(this);
        rlBlackList.setOnClickListener(this);
    }

    @Override
    public void initData() {
        setCacheInfo();
        marketUtil = new MarketUtil();
    }

    /**
     * 异步计算缓存大小
     */
    private void setCacheInfo() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                final double imageSize = FileSizeUtil.getFileOrFilesSize(RT.defaultImage, FileSizeUtil.SIZETYPE_MB);
                final double cacheSize = FileSizeUtil.getFileOrFilesSize(RT.defaultCache, FileSizeUtil.SIZETYPE_MB);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvCacheNumber.setText("(" + String.valueOf(imageSize + cacheSize) + ") MB");
                    }
                });
            }
        }).start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ibBack:
                finish();
            case R.id.rlMessage:
                break;
            case R.id.rlClearCache:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        UserManager.ins().cleanCache();
                        Unicorn.clearCache();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ToastUtil.showToast("清理成功");
                                tvCacheNumber.setText("");
                            }
                        });
                    }
                }).start();
                break;
            case R.id.rlScore:
                marketUtil.launchAppDetail(this, getPackageName(), "");
                break;
            case R.id.rlLogout:
                showLogoutDialog();
                break;
            case R.id.rlBlackList:
                BlackListActivity.start(this);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    public void showLogoutDialog() {
        if (tipDialog == null) {
            tipDialog = new TipDialog(this);
            tipDialog.setTextDes(getString(R.string.txt_exit, getString(R.string.app_name)));
            tipDialog.setButton1(RT.getString(R.string.see_others), new TipDialog.DialogButtonOnClickListener() {
                @Override
                public void onClick(View button, TipDialog dialog) {
                    WindowUtils.getInStance().clearList();
                    tipDialog.dismiss();
                }
            });
            tipDialog.setButton2(RT.getString(R.string.logout_enter), new TipDialog.DialogButtonOnClickListener() {
                @Override
                public void onClick(View button, TipDialog dialog) {
                    tipDialog.dismiss();
                    exit();
                }
            });
        }
        tipDialog.show();
    }

    private void exit() {
        UserManager.ins().logout(this);
    }
}
